import Vue from 'vue'

import 'normalize.css/normalize.css' // A modern alternative to CSS resets

import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
// import locale from 'element-ui/lib/locale/lang/en' // lang i18n

import '@/styles/index.scss' // global css

import App from './App'
import store from './store'
import router from './router'

import '@/icons' // icon
import '@/permission' // permission control
import '@/utils/component_use' // 进行组件的全局注册

/**
 * If you don't want to use mock-server
 * you want to use MockJs for mock api
 * you can execute: mockXHR()
 *
 * Currently MockJs will be used in the production environment,
 * please remove it before going online ! ! !
 */
// if (process.env.NODE_ENV === 'production') {
//   const { mockXHR } = require('../mock')
//   mockXHR()
// }

// set ElementUI lang to EN
// Vue.use(ElementUI, { locale })
// 如果想要中文版 element-ui，按如下方式声明
Vue.use(ElementUI)

Vue.config.productionTip = false

// 定义一个控制功能按钮权限的自定义指令
// 混入
Vue.directive('permission', {
  // inserted 使用指令得组件或DOM元素插入到页面上时执行
  inserted(el, binding) {
    // console.log(el, binding.value) // DOM元素  'add-employee'
    // 判断有无权限，由权限就不禁用，没权限就禁用
    const points = store.state.user.userInfo.roles.points || []
    // console.log(points)
    if (!points.includes(binding.value)) {
      // el.disabled = true
      el.style.display = 'none'
    }
  }
})

// 1. 定义混入(混入如何写？组件导出的对象怎样写，混入就怎么写)
const mixin = {
  methods: {
    hasPerm(point) {
      // console.log(point)
      // 1. 获取用户拥有的功能权限标识数组
      const points = store.state.user.userInfo.roles?.points || []
      // 2. 判断权限标识数组中是否有传进来的权限标识，有就返回true, 没有就返回false
      return points.includes(point)
    }
  }
}

// 2. 全局混入
Vue.mixin(mixin)

new Vue({
  el: '#app',
  router,
  store,
  render: h => h(App)
})
